package com.example.szys.controler;

import com.example.szys.POJO.Practice;
import com.example.szys.model.HomeWork;
import com.example.szys.POJO.HttpResult;
import com.example.szys.model.Score;
import com.example.szys.model.WareHouse;
import com.example.szys.repository.HomeWorkRepository;
import com.example.szys.repository.ScoreRepository;
import com.example.szys.repository.WareHouseRepository;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class HomeWorkControler {
    @Autowired
    private HomeWorkRepository homeWorkRepository;

    @Autowired
    private WareHouseRepository wareHouseRepository;

    @Autowired
    private ScoreRepository scoreRepository;

    @PostMapping(value = "/createhomework")
    public HttpResult<String> createHomeWork(@RequestParam("classid") String classid, @RequestParam("testname") String testname, @RequestParam("date") String date, @RequestParam("practice") String tmp){
        HttpResult<String> stringHttpResult=new HttpResult<>();
        HomeWork homeWork=new HomeWork();
        homeWork.setClassid(classid);
        Gson gson=new Gson();
        ArrayList<Practice> practices=gson.fromJson(tmp,new TypeToken<List<Practice>>(){}.getType());
        homeWork.setDate(date);
        homeWork.setTestname(testname);
        homeWork=homeWorkRepository.save(homeWork);
        int testid=homeWork.getId();
        for (Practice practice : practices){
            WareHouse wareHouse=new WareHouse();
            wareHouse.setTestid(testid);
            wareHouse.setQuestion(practice.getQuestion());
            wareHouse.setAnswer(practice.getAnswer());
            wareHouseRepository.save(wareHouse);
        }
        return stringHttpResult;
    }

    @PostMapping(value = "/gethomework")
    public HttpResult<ArrayList<com.example.szys.POJO.HomeWork>> getHomeWork(@RequestParam("classid") String classid){
        ArrayList<com.example.szys.POJO.HomeWork> homeWork=new ArrayList<>();
        List<HomeWork> homeWorkList=homeWorkRepository.findByClassid(classid);
        for (HomeWork homeWork1 : homeWorkList){
            com.example.szys.POJO.HomeWork homeWork2=new com.example.szys.POJO.HomeWork();
            homeWork2.setTestName(homeWork1.getTestname());
            homeWork2.setDate(homeWork1.getDate());
            homeWork2.setId(homeWork1.getId());
            homeWork.add(homeWork2);
        }
        HttpResult<ArrayList<com.example.szys.POJO.HomeWork>> httpResult=new HttpResult<>();
        httpResult.setContent(homeWork);
        return httpResult;
    }

    @PostMapping(value = "/getpractice")
    public HttpResult<ArrayList<Practice>> getPractice(@RequestParam("testid") int testid){
        HttpResult<ArrayList<Practice>> arrayListHttpResult=new HttpResult<>();
        ArrayList<Practice> practices=new ArrayList<>();
        List<WareHouse> tmp=wareHouseRepository.findByTestidOrderById(testid);
        for (WareHouse wareHouse : tmp){
            Practice p=new Practice();
            p.setQuestion(wareHouse.getQuestion());
            p.setAnswer(wareHouse.getAnswer());
            p.setId(wareHouse.getId());
            practices.add(p);
        }
        arrayListHttpResult.setContent(practices);
        return arrayListHttpResult;
    }

    @PostMapping(value = "/setscore")
    public HttpResult<Integer> setScore(@RequestParam("email") String email,@RequestParam("testid") int testid,@RequestParam("score") int score){
        HttpResult<Integer> integerHttpResult=new HttpResult<>();
        Score s=new Score();
        s.setEmail(email);
        s.setHomeid(testid);
        s.setScore(score);
        scoreRepository.save(s);
        integerHttpResult.setResult(1);
        return integerHttpResult;
    }

    @PostMapping(value = "/getscore")
    public HttpResult<Integer> getScore(@RequestParam("email") String email,@RequestParam("testid") int testid){
        HttpResult<Integer> integerHttpResult=new HttpResult<>();
        Score s=scoreRepository.findByEmailAndHomeid(email,testid);
        if(s==null){
            integerHttpResult.setResult(0);
            return integerHttpResult;
        }else {
            integerHttpResult.setResult(1);
            integerHttpResult.setContent(s.getScore());
            return integerHttpResult;
        }
    }
}
